/*
 * 一个主线程控制其他几个线程
 */ 
#include <iostream>
#include <pthread.h>
#include <unistd.h>
using namespace std;
pthread_mutex_t lock;
pthread_cond_t cond;
void* pthreadCall(void* arg)
{
    cout<<(const char*)arg<<" run..."<<endl;
    while(true)
    {
        pthread_cond_wait(&cond,&lock);
        cout<<"thread: "<<pthread_self()<<(const char*)arg<<endl;
    }
   return nullptr;
}
int main(void)
{
    //创建线程
    pthread_cond_init(&cond,nullptr);
    pthread_mutex_init(&lock,nullptr);

    pthread_t p1,p2,p3;
    pthread_create(&p1,NULL,pthreadCall,(void*)"thread 1");
    pthread_create(&p2,NULL,pthreadCall,(void*)"thread 2");
    pthread_create(&p3,NULL,pthreadCall,(void*)"thread 3");
    
    while(true)
    {
        getchar();
        pthread_cond_signal(&cond);//唤醒线程
    }
    pthread_cond_destroy(&cond);
    pthread_mutex_destroy(&lock);
    return 0;
}
